package com.huawei.petSystemdemo.utils;

import com.huawei.petSystemdemo.utils.HttpResultType;

import java.io.Serializable;

public class HttpResult<T> implements Serializable {
    int statusCode;
    String errMessage;
    T data;
    boolean success;

    public static HttpResult<Object> success(Object data) {
        return new HttpResult<>(data);
    }

    public static HttpResult<Object> failure(HttpResultType errType) {
        return new HttpResult<>(errType);
    }

    public static HttpResult<Object> failure(int errCode, String errMessage) {
        return new HttpResult<>(errCode, errMessage);
    }

    public HttpResult(int errCode, String errMessage) {
        this.statusCode = errCode;
        this.errMessage = errMessage;
        success = false;
    }

    public HttpResult(HttpResultType type) {
        if (type == HttpResultType.SUCCESS)
            throw new RuntimeException("类型错误");

        this.statusCode = type.getCode();
        this.errMessage = type.getName();
        success = false;
    }

    public HttpResult(T data) {
        HttpResultType type = HttpResultType.SUCCESS;
        this.statusCode = type.getCode();
        this.errMessage = type.getName();
        this.data = data;
        success = true;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getErrMessage() {
        return errMessage;
    }

    public void setErrMessage(String errMessage) {
        this.errMessage = errMessage;
    }
}
